{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [
    {
     "ename": "",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m在当前单元格或上一个单元格中执行代码时 Kernel 崩溃。\n",
      "\u001b[1;31m请查看单元格中的代码，以确定故障的可能原因。\n",
      "\u001b[1;31m单击<a href='https://aka.ms/vscodeJupyterKernelCrash'>此处</a>了解详细信息。\n",
      "\u001b[1;31m有关更多详细信息，请查看 Jupyter <a href='command:jupyter.viewOutput'>log</a>。"
     ]
    }
   ],
   "source": [
    "def hex_to_rgb(hex_color):\n",
    "    \"\"\"将十六进制颜色代码转换为RGB元组\"\"\"\n",
    "    hex_color = hex_color.lstrip('#')\n",
    "    return tuple(int(hex_color[i:i+2], 16) for i in (0, 2, 4))\n",
    "\n",
    "def generate_colormap(filename, min_elev, max_elev):\n",
    "    \"\"\"生成色彩映射表\n",
    "    \n",
    "    参数:\n",
    "    filename -- 输出文件名\n",
    "    min_elev -- DEM最小值\n",
    "    max_elev -- DEM最大值\n",
    "    \"\"\"\n",
    "    # 定义颜色分段规则（含陆地/海洋）\n",
    "    color_breaks = [\n",
    "        # 海洋部分\n",
    "        # (min_elev, -3000, '#081D58', '#000000'),  # 超深海\n",
    "        # (-3000, -200, '#2171B5', '#08306B'),      # 大陆坡\n",
    "        # (-200, 0, '#C7E9FF', '#6BAED6'),   \n",
    "        # (min_elev, -3000, '#000000','#081D58' ),  # 超深海\n",
    "        # (-3000, -200, '#08306B','#2171B5'),      # 大陆坡\n",
    "        # (-200, 0, '#6BAED6','#C7E9FF'),          # 浅海\n",
    "        (min_elev,0,'#5587bc','#90c6e8'),\n",
    "\n",
    "        \n",
    "        # 陆地部分\n",
    "        (0, 500, '#F0F8D4', '#B8D994'),           # 低海拔\n",
    "        (500, 2000, '#A6C96A', '#8A7247'),        # 中海拔\n",
    "        (2000, max_elev, '#6B4E35', '#FFFFFF')    # 高海拔（含雪线）\n",
    "    ]\n",
    "    \n",
    "    with open(filename, 'w') as f:\n",
    "        for z in range(min_elev, max_elev + 1):\n",
    "            # 寻找当前高程对应的颜色区间\n",
    "            for start, end, start_hex, end_hex in color_breaks:\n",
    "                if start <= z <= end:\n",
    "                    # 颜色插值计算\n",
    "                    start_rgb = hex_to_rgb(start_hex)\n",
    "                    end_rgb = hex_to_rgb(end_hex)\n",
    "                    \n",
    "                    # 计算插值比例（0~1之间）\n",
    "                    t = (z - start) / (end - start) if start != end else 0.5\n",
    "                    \n",
    "                    # 线性插值每个通道\n",
    "                    r = int(start_rgb[0] + (end_rgb[0] - start_rgb[0]) * t)\n",
    "                    g = int(start_rgb[1] + (end_rgb[1] - start_rgb[1]) * t)\n",
    "                    b = int(start_rgb[2] + (end_rgb[2] - start_rgb[2]) * t)\n",
    "                    \n",
    "                    # 写入文件（高程值 R G B）\n",
    "                    f.write(f\"{z} {r} {g} {b}\\n\")\n",
    "                    break\n",
    "\n",
    "# 使用示例（生成-11000到9000米的映射表）\n",
    "generate_colormap(r\"D:\\Lenovo\\Desktop\\云南大学\\毕业设计\\毕设数据\\输出数据\\色彩映射表\\dem_colormap3.clr\",-10923,8627)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "myenv2",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.18"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
